using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Collections;
using MyCollections.Model;
using MyCollections.Utils;
using MyCollections.DataBase;

namespace MyCollections.Forms
{
    public partial class addMovie : Form
    {
        private Movie movie;
        private bool checkedImbd;
        
        public addMovie()
        {
            InitializeComponent();

            movie = new Movie();

            // Set initial data
            this.checkedImbd = false;
            
            // load comboBox Format
            string file = Data.getMessage("file_movies_data");
            string xpath = "Movies/format/*";
            XmlNodeList xmlNodeList = Xml.GetXmlNodeList(file, xpath);
            for (int i = 0; i < xmlNodeList.Count; i++)
            {
                format.Items.Add(xmlNodeList[i].InnerText);
            }

            // Disabled Buttons 
            this.bt_add.Enabled = false;
        }

        private void setInitialData()
        {
            this.movie = new Movie();
            this.title.Text = "";
            this.format.Text = "";
            imdb_actors.Text = "";
            imdb_director.Text = "";
            imdb_genre.Text = "";
            imdb_language.Text = "";
            imdb_rating.Text = "";
            imdb_url.Text = "";
            imdb_writer.Text = "";
            imdb_year.Text = "";
            pb.ImageLocation = "";

            bt_imdb_nok.Enabled = false;
            bt_imdb_link.Enabled = false;
            bt_add.Enabled = false;
        }

        private void loadMovie(string idMovie)
        {
            if (String.IsNullOrEmpty(idMovie))
            {
                // connect to ImdbWebService and get information from movie
                this.movie.imdb = ImdbTools.getImdbInfo(this.title.Text);
            }
            else 
            {
                this.movie.imdb = ImdbTools.getImdbInfoById(idMovie);
            }

            if (this.movie.imdb != null)
            {
                title.Text = this.movie.imdb.title;
                imdb_actors.Text = "";
                imdb_director.Text = this.movie.imdb.director;
                imdb_genre.Text = "";
                foreach (string s in this.movie.imdb.genre)
                {
                    if(!imdb_genre.Text.Contains(s))
                        imdb_genre.Text += s + ", ";
                }
                imdb_language.Text = this.movie.imdb.language;
                imdb_rating.Text = "";
                imdb_url.Text = this.movie.imdb.url;
                imdb_writer.Text = this.movie.imdb.writer;
                imdb_year.Text = this.movie.imdb.year;
                pb.Image = ImdbTools.LoadPicture(this.movie.imdb.pictureurl);

                bt_imdb_nok.Enabled = true;
                bt_imdb_link.Enabled = true;
                bt_add.Enabled = true;
            }
        }

        #region Events
        private void bt_close_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void bt_loadMovie_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.title.Text))
            {
                MessageBox.Show(Data.getMessage("alert_findMovie"));
                this.title.Focus();
            }
            else 
            {
                loadMovie(null);
            }
        }

        private void bt_imdb_link_Click(object sender, EventArgs e)
        {
            webbrowser n = new webbrowser(new Uri(imdb_url.Text), false);
            n.ShowInTaskbar = false;
            n.Show();
        }

        private void bt_imdb_nok_Click(object sender, EventArgs e)
        {
            this.checkedImbd = false;
            this.bt_add.Enabled = false;

            webbrowser n = new webbrowser(null, true);
            if (n.ShowDialog() == DialogResult.OK)
            {
                string urlMovie = ((WebBrowser)n.Controls["webBrowser1"]).Url.ToString();
                string idMovie = urlMovie.Substring(urlMovie.Length-10, 9);

                setInitialData();
                loadMovie(idMovie);
            }            
        }

        private void bt_clear_Click(object sender, EventArgs e)
        {
            this.setInitialData();
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            //save data
            /*SQLite_Movie sqlite = new SQLite_Movie();
            sqlite.save(this.movie);
            this.DialogResult = DialogResult.OK;*/
        }
        #endregion
    }
}